<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">区域管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>区域管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：<input id="area-edit-search" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="area-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="area-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="area-table" lay-filter="area-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="area-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="add">添加下级区域</a>
</script>

<script>
    layui.use(['form', 'table', 'util', 'admin', 'treetable'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var admin = layui.admin;
        var treetable = layui.treetable;

        /** 渲染表格:插件开源地址：https://gitee.com/whvse/treetable-lay **/
        var renderTable = function () {
            treetable.render({
                treeColIndex: 1, // 树形图标显示在第几列
                treeSpid: -1, // 最上级的父级id
                treeIdName: 'id', // id字段的名称
                treePidName: 'parentId', // pid字段的名称
                treeDefaultClose: true, // 是否默认折叠
                treeLinkage: false, // 父级展开时是否自动展开所有子级
                elem: '#area-table',
                url: 'organization/area/list',
                page: false,
                cols: [
                    [
                        {type: 'numbers'},
                        {field: 'name', title: '区域名称'},
                        {field: 'code', title: '区域编码'},
                        {
                            field: 'type', align: 'center', templet: function (d) {
                                if (d.type == 1) {
                                    return '<span class="layui-badge layui-bg-blue">国家</span>';
                                } else if (d.type == 2) {
                                    return '<span class="layui-badge-rim layui-bg-orange">省份、直辖市</span>';
                                } else if (d.type == 3) {
                                    return '<span class="layui-badge-rim">地市</span>';
                                } else if (d.type == 4) {
                                    return '<span class="layui-badge-rim">区县</span>';
                                }
                            }, title: '类型'
                        },
                        {field: 'sort', align: 'center', title: '排序号'},
                        {templet: '#area-state', width: 250, align: 'center', title: '操作'}
                    ]
                ]
            });
        };

        renderTable();

        /** 工具条点击事件 **/
        table.on('tool(area-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'add') { // 添加下级
                var nDate = {parentId: obj.data.id};
                showEditModel(nDate);
            } else if (layEvent === 'del') { // 删除
                layer.confirm('确定删除此区域吗？', function () {
                    layer.load(2);
                    $.post('organization/area/delete', {
                        areaId: obj.data.id
                    }, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 200) {
                            layer.msg(data.msg, {icon: 1});
                            renderTable();
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    });
                });
            }
        });

        /** 添加 **/
        $('#area-btn-add').click(function () {
            showEditModel();
        });

        // 显示表单弹窗
        var showEditModel = function (data) {
            var title = (data && data.id) ? '修改区域' : '添加区域';
            admin.putTempData('t_area', data);
            admin.popupCenter({
                title: title,
                path: 'organization/area/editForm',
                finish: function () {
                    renderTable();
                }
            });
        };

        // 搜索按钮点击事件
        $('#area-btn-search').click(function () {
            var keyword = $('#area-edit-search').val();
            var searchCount = 0;
            $('#area-table').next('.treeTable').find('.layui-table-body tbody tr td').each(function () {
                $(this).css('background-color', 'transparent');
                var text = $(this).text();
                if (keyword != '' && text.indexOf(keyword) >= 0) {
                    $(this).css('background-color', 'rgba(250,230,160,0.5)');
                    if (searchCount == 0) {
                        $('.layui-tab-item.layui-show').stop(true);
                        $('.layui-tab-item.layui-show').animate({scrollTop: $(this).offset().top - 150}, 500);
                    }
                    searchCount++;
                }
            });
            if (keyword != '' && searchCount == 0) {
                layer.msg("没有匹配结果", {icon: 5});
            } else {
                treetable.expandAll('#area-table');
            }
        });
    });
</script>